<?php
/**
 * Created by PhpStorm.
 * User: 式神 (luck48.com)
 * Email: 289650682@qq.com
 * Name: ${NAME}Administrator
 * Date: 2017-03-23
 * Time: 15:57
 */
namespace App\Http\Controllers\Admin;

use App\Http\Models\Category;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class CategoryController extends CommonController
{
    //栏目列表
    public function categoryList(Request $request){
        if($request->isMethod('post')){
            $json=[];
            $model=new Category();
            $list=$model->orderBy('listsort','asc')->get();
            $list=catlist($list);
            $data['list']=$list;
            $data['title']=trans('config.listtitle');
            $json['status']=200;
            $json['data']=$data;
            return $json;
        }
        return view('admin.category.list');
    }

    //添加栏目
    public function categoryAdd(Request $request){
        if($request->isMethod('post')){
            $input=Input::except('_token');
            $input['catdir']=preg_replace('/\s/', '_', strtolower($input['catdir']));
            if(Category::create($input)){
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                $data['url']='reload';
            }else{
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }
        $cate=Category::all();
        $cates=catlist($cate);
        if(!$request->get('pid')){
            $pid=0;
        }else{
            $pid=$request->get('pid');
        }
        $template=template(config('system.template'));
        return view('admin.category.add',compact('cates','pid','template'));
    }

    //添加栏目
    public function categoryEdit(Request $request){
        if($request->isMethod('post')){
            $input=Input::except('_token','id','file');
            if(isset($input['catdir'])){
                $input['catdir']=preg_replace('/\s/', '_', strtolower($input['catdir']));
            }
            if(Category::where('id',Input::get('id'))->update($input)){
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                if(isset($input['catdir'])){
                    $data['url']='reload';
                }
            }else{
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }

            return $data;
        }
        $model=new Category();
//        $cate=$model->all();
//        $cates=catlist($cate);
        $id=Input::get('id')?Input::get('id'):0;
        if($id==0){
            return trans('config.return_error');
        }
        $data=$model->where('id',$id)->first();
        $list=$model->orderBy('listsort','asc')->where('showed',1)->get();
        $ids=getChildsId($list,$id);
        array_push($ids,$id);
        $cates=catlist($list);
        $template=template(config('system.template'));
        return view('admin.category.edit',compact('cates','ids','data','template'));
    }
    //删除栏目
    public function categoryDel(Request $request){
        if($request->isMethod('post')){
            $id=Input::get('id');
            $id=explode(",", $id);
            $m=new Category();
            DB::beginTransaction();
            try{
                $m->whereIn('id',$id)->delete();
                $d=[
                    'pid'=>0
                ];
                $m->whereIn('pid',$id)->update($d);
                DB::commit();
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                $data['url']='reload';
            }catch (\Exception $e) {
                DB::rollBack();
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }
    }
}


